/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.jrh.web.admin.form;

import java.util.ArrayList;
import java.util.List;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.lang.StringUtils;

import com.jrh.domain.entity.Atlas;
import com.jrh.domain.entity.AtlasDetail;
import com.jrh.domain.entity.Category;
import com.jrh.domain.entity.FeatureValue;
import com.jrh.domain.entity.Label;
import com.jrh.domain.entity.Mall;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.Showroom;

/**
 * form 转换类
 * 
 * @author jasen.hsu.cn@gmail.com 2012-7-26 下午5:05:45
 */
public class AdminFormCopier {

    private static BeanCopier atlasForm2Do       = BeanCopier.create(AdminAtlasForm.class, Atlas.class, false);

    private static BeanCopier atlasDetailForm2Do = BeanCopier.create(AdminAtlasDetailForm.class, AtlasDetail.class,
                                                                     false);

    private static BeanCopier categoryForm2Do    = BeanCopier.create(AdminCategoryForm.class, Category.class, false);

    private static BeanCopier productForm2Do     = BeanCopier.create(AdminProductForm.class, Product.class, false);

    private static BeanCopier mallForm2Do        = BeanCopier.create(AdminMallForm.class, Mall.class, false);
   
    private static BeanCopier showroomForm2Do    = BeanCopier.create(AdminShowroomForm.class, Showroom.class, false);

    private static BeanCopier labelForm2Do       = BeanCopier.create(AdminLabelForm.class, Label.class, false);
    
    public static Label convert(AdminLabelForm labelForm, Label label) {
        if (labelForm == null) return label;
        if (label == null) label = new Label();
        labelForm2Do.copy(labelForm, label, null);
        return label;
    }

    public static Atlas convert(AdminAtlasForm atlasForm, Atlas atlas) {
        if (atlasForm == null) return atlas;
        if (atlas == null) atlas = new Atlas();
        atlasForm2Do.copy(atlasForm, atlas, null);
        atlas.setCoverImg(atlasForm.getCoverImgPath());
        return atlas;
    }

    public static Category convert(AdminCategoryForm categoryForm, Category category) {
        if (categoryForm == null) return category;
        if (category == null) category = new Category();
        categoryForm2Do.copy(categoryForm, category, null);
        // 父类目ID为最顶层0
        if (Category.PARENT_CAT.equals(categoryForm.getParent())) {
            category.setIsParent(Category.IS_PARENT_);
        } else {
            category.setIsParent(Category.IS_CHILD_);
        }
        return category;
    }

    public static AtlasDetail convert(AdminAtlasDetailForm atlasDetailForm, AtlasDetail atlasDetail) {
        if (atlasDetailForm == null) return atlasDetail;
        if (atlasDetail == null) atlasDetail = new AtlasDetail();
        atlasDetailForm2Do.copy(atlasDetailForm, atlasDetail, null);
        return atlasDetail;
    }

    public static Product convert(AdminProductForm adminProductForm, Product product) {
        if (adminProductForm == null) return product;
        if (product == null) product = new Product();
        productForm2Do.copy(adminProductForm, product, null);
        if (product.getFeatureValueList() == null) {
            return product;
        }
        List<FeatureValue> result = new ArrayList<FeatureValue>();
        for (FeatureValue featureValue : product.getFeatureValueList()) {
            if (featureValue == null) {
                continue;
            }
            if (StringUtils.isEmpty(featureValue.getCode())) {
                continue;
            }
            String[] strs = featureValue.getCode().split("_");
            if (strs != null && strs.length == 2) {
                FeatureValue fv = new FeatureValue();
                try {
                    fv.setFeatureId(Long.parseLong(strs[0]));
                    fv.setId(Long.parseLong(strs[1]));
                    result.add(fv);
                } catch (Exception e) {
                }
            }
        }
        product.setFeatureValueList(result);
        return product;
    }

    public static Mall convert(AdminMallForm mallForm, Mall mall) {
        if (mallForm == null) return mall;
        if (mall == null) mall = new Mall();
        mallForm2Do.copy(mallForm, mall, null);
        mall.setLogoUrl(mallForm.getLogoUrl());
        return mall;
    }

    public static Showroom convert(AdminShowroomForm showroomForm, Showroom showroom) {
    	if (showroomForm == null) return showroom;
    	if (showroom == null) showroom= new Showroom();
    	showroomForm2Do.copy(showroomForm, showroom, null);
    	showroom.setMainPic(showroomForm.getMainPicPath());
    	showroom.setSmallPic(showroomForm.getSmallPicPath());
    	return showroom;
    }

}
